<?php

namespace App\Http\Requests;

use App\Models\ProductSku;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class OrderRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        //判断用户提交的地址ID 是否存在于数据库的当前用户
        return [
            'address_id' => [
                "required",
                Rule::exists("user_addresses","id")->where("user_id",$this->user()->id),
            ],
            "items" => ["required","array"],
            "items.*.sku_id" => [
                //检查items数组下每一个子数组的sku_id参数
                "required",
                function($attribute,$value,$fail){
                    if (!$sku = ProductSku::find($value)) {
                        return $fail('该商品不存在');
                    }
                    if (!$sku->product->on_sale) {
                        return $fail('该商品未上架');
                    }
                    if ($sku->stock === 0) {
                        return $fail('该商品已售完');
                    }

                    //获取当前索引
                    preg_match("/items\.(\d+)\.sku_id/",$attribute,$m);

                    $index = $m[1];
                    //根据索引找到用户所提交的购买数量
                    
                    $amonut = $this->input("items")[$index]["amount"];
                    if($amonut > 0 && $amonut > $sku->stock){
                        return $fail("该商品库存不足");
                    }
                }
            ],
            'items.*.amount' => ['required', 'integer', 'min:1'],
        ];
   
    }
}
